function ShowSpare1Fix( ply )

	ply:ConCommand( "gm_showspare1\n" );

end
concommand.Add( "gm_spare1", ShowSpare1Fix );

function ShowSpare2Fix( ply )

	ply:ConCommand( "gm_showspare2\n" );

end
concommand.Add( "gm_spare2", ShowSpare2Fix );

function ccGetChars( ply, cmd, arg )

	if( #arg ~= 1 ) then	
		return	
	end
	
	local uniqueid = arg[1]
	
	if( tonumber( uniqueid ) ~= ply:GetField( "UniqueID" ) ) then
		return
	end
		
	local result = sql.Query( "SELECT RPName, Model, id, Title, SecondTitle FROM `characters` WHERE SteamID = '" .. ply:SteamID() .. "'" )

	local delay = 0
	
	if( !result ) then
		return 0
	end	
	
	for k, v in pairs( result ) do
	
	local char = v
		
		if( tonumber( char["id"] ) ~= tonumber( ply:GetField( "charid" ) ) ) then
		
			local function AddChar( name, model, id, title, stitle )
			
				umsg.Start( "addCharacter", ply )
					umsg.String( name )
					umsg.String( model )
					umsg.Short( id )
					umsg.String( title )
					umsg.String( stitle )
				umsg.End();
					
				end	
		
			timer.Simple( delay, AddChar, char["RPName"], char["Model"], char["id"], char["Title"], char["SecondTitle"] )
			
			delay = delay + .15
			
		end
	
	end

end
concommand.Add( "eng_getchars", ccGetChars );

function ccCloseCharMenu( ply, cmd, arg )
	
	ply:UnLock();
	ply:DrawViewModel( true );
	ply:CallEvent( "MapViewOff" );
	
	umsg.Start( "RCM", ply ); umsg.End();	

end
concommand.Add( "eng_closecharmenu", ccCloseCharMenu );

function ccOpenContrabandMenu( ply, cmd, arg )

	if( string.find( ply:GetPlayerCharacterFlags(), "C" ) ) then
		ply:OpenContrabandMenu();
		ply:GetTable().ContrabandMenu = true;
	else
		umsg.Start( "NOBM", ply );
		umsg.End();
	end	
		
end
concommand.Add( "eng_opencb", ccOpenContrabandMenu );

function ccCloseContrabandMenu( ply, cmd, arg )

	ply:GetTable().ContrabandMenu = false;
	umsg.Start( "RCBM", ply );
	umsg.End();

		
end
concommand.Add( "eng_closecb", ccCloseContrabandMenu );

function ccOpenBlackmarketMenu( ply, cmd, arg )

	if( string.find( ply:GetPlayerCharacterFlags(), "X" ) ) then
		ply:OpenBlackmarketMenu();
		ply:GetTable().BlackmarketMenu = true;
	else
		umsg.Start( "NOBM", ply );
		umsg.End();
	end	
		
end
concommand.Add( "eng_openbm", ccOpenBlackmarketMenu );

function ccCloseBlackmarketMenu( ply, cmd, arg )

	ply:GetTable().BlackmarketMenu = false;
	umsg.Start( "RBMM", ply );
	umsg.End();
		
end
concommand.Add( "eng_closebm", ccCloseBlackmarketMenu );

function ccOpenLoanMenu( ply, cmd, arg )

	umsg.Start( "OLM", ply ) umsg.End()
	
end
concommand.Add( "eng_olm", ccOpenLoanMenu )	

function ccOpenWholesaleMenu( ply, cmd, arg )

	if( string.find( ply:GetPlayerCharacterFlags(), "U" ) ) then

		ply:OpenWholesaleMenu();
		ply:GetTable().WholesaleMenu = true;
		
	else

		ply:PrintMessage( 3, "You are not a USI member! If you wish to buy an item find the USI store." )
		
	end	
		
end
concommand.Add( "eng_openws", ccOpenWholesaleMenu );

function ccCloseWholesale( ply, cmd, arg )

	ply:GetTable().WholesaleMenu = false;
	umsg.Start( "RWM", ply );
	umsg.End();
		
end
concommand.Add( "eng_closews", ccCloseWholesale );

function CCDestroyCrate( ply, cmd, args )
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	local itemflags = ent:GetTable().ItemData.Flags
	
	local pos = ent:GetPos();
	local ang = ent:GetAngles();
	local model = ent:GetTable().ItemData.Model
	
	if( ent:IsItem() ) then
		if( string.find( itemflags, "b" ) ) then
			ent:Remove();
			local fakeitem = ents.Create( "prop_physics" );
			fakeitem:SetPos( pos );
			fakeitem:SetAngles( ang );
			fakeitem:SetModel( model );
			fakeitem:Spawn();
			fakeitem:Fire( "break", "", 1 );
		end
		
	end

end
concommand.Add( "eng_destroycrate", CCDestroyCrate );

function CCEmptyCrate( ply, cmd, args )

	if( type( ent ) == "table" ) then
	
		local itemdata = { }
		
		for k, v in pairs( ent ) do
			itemdata[k] = v;
		end
		
		ent = { }
		ent.tbl = { }
		ent.tbl.ItemData = itemdata;
		
		ent.GetTable = function( self )
			return self.tbl;
		end
	
	end

	local delay = 0;
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	local itemflags = ent:GetTable().ItemData.Flags
	
	
	if( ent:IsItem() ) then
	
		if( string.find( itemflags, "c" ) ) then
	for x, v in pairs( ent:GetTable().ItemData.InventoryGrid ) do
	
		for y, k in pairs( v ) do
			
			if( k.ItemData ) then
				
				local id = k.ItemData.ID;
				
				local give = function() 
				
					if( not ply:GetTable().ItemsDownloaded[id] ) then 
						ply:SendItemData( id );
					end
					
					if( ent:GetTable().ItemData.InventoryGrid[x][y].ItemData and
						ent:GetTable().ItemData.InventoryGrid[x][y].ItemData.ID == id ) then
						
						ent:GetTable().ItemData:RemoveItemAt( x, y );
						TS.CreateItemProp( id, ent:GetPos() + Vector( 0, 0, 50 ) );
					
					end
					
				end
				timer.Simple( delay, give );
				
				delay = delay + 1;
				
			end
			
		end
	
	end
		end
		
	end


end
concommand.Add( "eng_emptycrate", CCEmptyCrate );

function CCSearchPlayer( ply )

	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 200;
	trace.filter = ply;
	local tr = util.TraceLine( trace );
	
	local searchtime = 5
	
	if( ValidEntity( tr.Entity ) and tr.Entity:IsPlayer() ) then
	
	local cansearch = true;
		
		if( tr.Entity:GetTable().BeingSearched ) then
		
			cansearch = false;
		
		end
		
		if( tr.Entity:GetTable().StanceATW ) then
		
			--Check if facing the player's base/possibly if player is allowing tie
			searchtime = 3
		
		end
		
	if( cansearch ) then
		
			tr.Entity:GetTable().BeingSearched = true;
			tr.Entity:GetTable().BeingSearchedBy = ply;
			ply:GetTable().Searching = true;
			ply:GetTable().SearchingTarget = tr.Entity;
	
	local thinkfunc = function( ply, done )
			
				local destroybars = function()
				
					if( ply:IsValid() ) then
					
						ply:GetTable().Searching = false;
						DestroyProcessBar( "searching", ply );
					
					end
					
					if( tr.Entity:IsValid() ) then
					
						tr.Entity:GetTable().BeingSearched = false;
						DestroyProcessBar( "beingsearched", tr.Entity );
						
					end
				
				end
				
				if( not ply:IsValid() or not tr.Entity:IsValid() ) then
				
					destroybars();
					return;
				
				end
			
				if( ply:GetTable().SearchPos ~= ply:GetPos() or
					tr.Entity:GetTable().SearchPos ~= tr.Entity:GetPos() ) then
				
					destroybars();
					return;
					
				end
				
				if( not ply:Alive() or not tr.Entity:Alive() ) then
				
					destroybars();
					return;
				
				end
				
				if( done ) then
				
					tr.Entity:SearchedBy( ply );
					destroybars();
				
				end
				
			end
	
	ply:GetTable().SearchPos = ply:GetPos();
	tr.Entity:GetTable().SearchPos = tr.Entity:GetPos();

	CreateProcessBar( "searching", "Searching", ply );
		SetEstimatedTime( searchtime, ply );
		SetThinkDelay( .33, ply );
		SetThink( thinkfunc, ply );
		SetColor( Color( 41, 157, 20, 200 ), ply );
	EndProcessBar( ply );
			
	CreateProcessBar( "beingsearched", "Being searched", tr.Entity );
		SetEstimatedTime( searchtime, tr.Entity );
		SetThinkDelay( .33, tr.Entity );
		SetColor( Color( 41, 157, 20, 200 ), tr.Entity );
	EndProcessBar( tr.Entity );
	
	end
	
	end

end
concommand.Add( "eng_search", CCSearchPlayer );

function CCSetPropertyName( ply, cmd, args )

	if( not args[1] ) then return; end
	
	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 200;
	trace.filter = ply;
	local tr = util.TraceLine( trace );
	
	if( ValidEntity( tr.Entity ) and tr.Entity:IsDoor() ) then
	
		if( tr.Entity:OwnsDoor( ply ) ) then
			
			local parent = tr.Entity:GetTable().PropertyParent;
			local propertyname = tr.Entity:GetTable().PropertyName;
			
			if( propertyname ~= args[1] ) then
			
			if( propertyname == "Combine Civil Housing" ) then
			
				ply:PrintMessage( 3, "Cannot change the Property name of this door" );
				
			else
			
			tr.Entity:GetTable().PropertyName = args[1]
				
			for k, v in pairs( TS.MapDoors ) do
				if( v.PropertyParent == parent and not string.find( v.Door:GetTable().DoorFlags, "s" ) ) then	
					v.Door:GetTable().PropertyName = args[1]
				end
			end
				
				ply:PrintMessage( 3, "Property name set to " .. args[1] );
				
			end
			end
			
		else
		
			ply:PrintMessage( 3, "You don't own this door" );
		
		end
	
	end

	return "";
	
end
concommand.Add( "eng_setpropertyname", CCSetPropertyName );

function CCBuyBMItem( ply, cmd, args )

	if( not args[1] ) then return; end
	
	local id = args[1]
	local price = TS.ItemsData[id].Price
	local name = TS.ItemsData[id].Name
	local x = tonumber( args[2] )
	local y = tonumber( args[3] )
	
	if( not ply:CanAfford( price ) ) then return; end
	
	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 150;
	trace.filter = ply;
	
	local tr = util.TraceLine( trace );
	
	local purchaseitemthink = function( ply, done )
		
		if( done ) then
			
				RemoveBlackmarketItemAt( x, y );
				--TS.CreateItemProp( args[1], tr.HitPos );
				ply:GiveAnyInventoryItem( args[1] )
				ply:TakeMoney( price );
				ply:PrintMessage( 3, "Purchased " .. name .. " for " .. price .. " credits!" )
				ply:PrintMessage( 3, "Item has been placed in inventory" );
				bmitemtotals = bmitemtotals - 1
			
				DestroyProcessBar( "purchaseitem", ply );

			return;			
		
		end
	
	end
	
	CreateProcessBar( "purchaseitem", "Purchasing...", ply );
		SetEstimatedTime( 2, ply );
		SetThinkDelay( 2, ply );
		SetThink( purchaseitemthink, ply );
	EndProcessBar( ply );	
	
end
concommand.Add( "eng_buybmitem", CCBuyBMItem );

function CCBuyCBItem( ply, cmd, args )

	if( not args[1] ) then return; end
	
	local id = args[1]
	local price = TS.ItemsData[id].Price
	local name = TS.ItemsData[id].Name
	local x = tonumber( args[2] )
	local y = tonumber( args[3] )
	
	if( not ply:CanAfford( price ) ) then return; end
	
	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 150;
	trace.filter = ply;
	
	local tr = util.TraceLine( trace );
	
	local purchaseitemthink = function( ply, done )
		
		if( done ) then
			
				RemoveContrabandItemAt( x, y );
				--TS.CreateItemProp( args[1], tr.HitPos );
				ply:GiveAnyInventoryItem( args[1] )
				ply:TakeMoney( price );
				ply:PrintMessage( 3, "Purchased " .. name .. " for " .. price .. " credits!" )
				ply:PrintMessage( 3, "Item has been placed in inventory" );
				cbitemtotals = cbitemtotals - 1
			
				DestroyProcessBar( "purchaseitem", ply );

			return;			
		
		end
	
	end
	
	CreateProcessBar( "purchaseitem", "Purchasing...", ply );
		SetEstimatedTime( 2, ply );
		SetThinkDelay( 2, ply );
		SetThink( purchaseitemthink, ply );
	EndProcessBar( ply );	
	
end
concommand.Add( "eng_buycbitem", CCBuyCBItem );

function CCBuyWholesaleItem( ply, cmd, args )

	if( not args[1] ) then return; end
	
	local id = args[1]
	local price = TS.ItemsData[id].Price
	local name = TS.ItemsData[id].Name
	local x = tonumber( args[2] )
	local y = tonumber( args[3] )
	
	if( not ply:CanAfford( price ) ) then return; end
	
	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 150;
	trace.filter = ply;
	
	local tr = util.TraceLine( trace );
	
	--local stillthere, x, y = HasWholesaleItem( id )
	
	
	local purchaseitemthink = function( ply, done )
		
		if( done ) then
			
				RemoveWholesaleItemAt( x, y );
				--TS.CreateItemProp( args[1], tr.HitPos );
				ply:GiveAnyInventoryItem( args[1] )
				ply:TakeMoney( price );
				ply:PrintMessage( 3, "Purchased " .. name .. " for " .. price .. " credits!" )
				ply:PrintMessage( 3, "Item has been placed in inventory" );
				wsitemtotals = wsitemtotals - 1
			
				DestroyProcessBar( "purchaseitem", ply );

			return;			
		
		end
	
	end
	
	CreateProcessBar( "purchaseitem", "Purchasing...", ply );
		SetEstimatedTime( 2, ply );
		SetThinkDelay( 2, ply );
		SetThink( purchaseitemthink, ply );
	EndProcessBar( ply );	
	
end
concommand.Add( "eng_buywholesaleitem", CCBuyWholesaleItem );

function CCSetDoorName( ply, cmd, args )

	if( not args[1] ) then return; end
	
	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 200;
	trace.filter = ply;
	local tr = util.TraceLine( trace );
	
	if( ValidEntity( tr.Entity ) and tr.Entity:IsDoor() ) then
	
		if( tr.Entity:OwnsDoor( ply ) ) then
			
			local doorname = tr.Entity:GetTable().DoorName;
			
			if( doorname ~= args[1] ) then
			
				tr.Entity:GetTable().DoorName = args[1]
				
				ply:PrintMessage( 3, "Door name set to " .. args[1] );
				
			end
			
			umsg.Start( "RNM", ply );
			umsg.End();
		
		else
		
			ply:PrintMessage( 3, "You don't own this door" );
		
		end
	
	end

	return "";
	
end
concommand.Add( "eng_setdoorname", CCSetDoorName );

function CCPromptPropName( ply )

	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 200;
	trace.filter = ply;
	local tr = util.TraceLine( trace );
	
	if( ValidEntity( tr.Entity ) and tr.Entity:IsDoor() ) then
	if( tr.Entity:GetTable().MainOwner == ply ) then
		umsg.Start( "PNM", ply );
			umsg.String( tr.Entity:GetTable().PropertyName or "" );
			umsg.String( tr.Entity:GetTable().DoorName or "" );
		umsg.End();
	else
			ply:PrintMessage( 3, "You are not the main owner, you cannot change the propertys name!" );
	end
	
	end
	
end
concommand.Add( "eng_promptpropname", CCPromptPropName );

function ccTakeLoan( ply, cmd, arg )
	
	if( not tonumber( arg[1] ) ) then 
		return;
	end
	
	local loan = tonumber( arg[1] )
	local maxloan = 500
	
	if( not (( ply:GetPlayerLoans() + loan ) > maxloan ) ) then
		
		ply:SetPlayerLoans( ply:GetPlayerLoans() + loan )
		ply:SetPlayerMoney( ply:GetPlayerMoney() + loan )
		Console( ply, "You took out a " .. loan .. " credit loan, make sure you pay it off.", true );
		--return;
		
		ply:SaveData( "Loan", arg[1] )
		
	elseif( loan >= maxloan ) then
		
		Console( ply, "You can only have ".. maxloan .. " credits out from loans at one time.", true );
		--return;
	
	else
	
		Console( ply, "You already owe " .. maxloan .. " credits, pay this off before taking more.", true );
		--return;
		
	end
	
end
concommand.Add( "eng_takeloan", ccTakeLoan );

function ccPayLoan( ply, cmd, args )
	
	if( not tonumber(args[1]) ) then return; end
	
	local payed = tonumber( args[1] );
	local owed = ply:GetPlayerLoans();
	
	if( not (payed > owed) and (payed > 0) ) then
		
		ply:SetPlayerMoney( ply:GetPlayerMoney() - payed );
		ply:SetPlayerLoans( owed - payed )
		Console( ply, "You payed " .. payed .. " credits off towards your loan of " .. owed .. " credits.", true );
		ply:SaveData( "Loan", ply:GetPlayerLoans() - arg[1] )
		return;
			
	else
	
		Console( ply, "You can't pay more than you owe!", true );
		return;
		
	end
	
end
concommand.Add( "eng_payloan", ccPayLoan );

function ccLoanAmount( ply, cmd, arg )

	Console( ply, "You owe " .. ply:GetPlayerLoans() .. " credits to the Universal Union", true )
	
end
concommand.Add( "eng_loanamount", ccLoanAmount ) 	

function CCAddDoorOwner( ply, cmd, args )

	if( not args[1] ) then return; end
	
	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 200;
	trace.filter = ply;
	local tr = util.TraceLine( trace );
	
	if( ValidEntity( tr.Entity ) and tr.Entity:IsDoor() ) then
	
		if( tr.Entity:OwnsDoor( ply ) ) then
			
			local name = args[1];
			local succ, result = TS.FindPlayerByName( name );
			TS.ErrorMessage( ply, true, succ, result );
			
				if( succ ) then
				
					if( tr.Entity:OwnsDoor( result ) ) then
					
						ply:PrintMessage( 3, result:GetRPName() .. " already co-owns this door" );
						umsg.Start( "RADO", ply );
						umsg.End();
					
					else
				
						result:OwnDoor( tr.Entity );
						ply:PrintMessage( 3, result:GetRPName() .. " has been made co-owner of this door" );
						umsg.Start( "RADO", ply );
						umsg.End();
					
					end
					
				end
		
		else
		
			ply:PrintMessage( 3, "You don't own this door" );
		
		end
	
	end

	return "";
	
end
concommand.Add( "eng_ado", CCAddDoorOwner );

function CCPromptAddDoorOwner( ply )

	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 200;
	trace.filter = ply;
	local tr = util.TraceLine( trace );

		if( tr.Entity:GetTable().MainOwner == ply ) then
			umsg.Start( "PADO", ply );
			umsg.End();
		else
			ply:PrintMessage( 3, "You are not the main owner, you cannot give out spare keys!" );
		end

end
concommand.Add( "eng_promptado", CCPromptAddDoorOwner );

function ccChangeFrequency( ply, cmd, args )

	if( #args < 1 ) then return; end

	if( ply:IsCombine() ) then
		ply:PrintMessage( 3, "Combine cannot change their frequency!" );
		return;
	end
	
	if( not ply:HasItem( "radio" ) ) then
		ply:PrintMessage( 3, "You need a radio to set its frequency." );
		return;
	end

	--ply:SetField( "radiofreq", math.Round( tonumber( args[1] ) * 100 ) / 100 );
	ply:SetPlayerRadioFreq( math.Round( tonumber( args[1] ) * 100 ) / 100 )
	ply:PrintMessage( 3, "You are now tuned into frequency " .. math.Round( tonumber( args[1] ) * 100 ) / 100 .. "!" );
	umsg.Start( "RRM", ply );
	umsg.End();

end
concommand.Add( "eng_changefrequency", ccChangeFrequency );

function CCPromptRadioMenu( ply )
	
	if( ply:HasItem( "radio" ) ) then
		umsg.Start( "PRM", ply );
		umsg.End();
	else
		umsg.Start( "NR", ply );
		umsg.End();	
	end
	
end
concommand.Add( "eng_promptradiomenu", CCPromptRadioMenu );

function ccCreateCharacter( ply, cmd, arg )

	local faction = arg[1];
	local name = arg[2];
	local age = arg[3];
	local title = arg[4];
	local title2 = arg[5];
	local model = arg[6];
	
	print( arg[6] )
	
	for k, v in pairs( HL2Factions ) do
		if( string.find( k, faction ) ) then
			if( not table.HasValue( v.Models, string.lower( model ) ) ) then
				ply:PrintMessage( 3, "Using invalid model" )
				--print( model )
				return	
			end
		end
	end	
	
	ply:SetNWString( "RPName", name );
	ply:SetPlayerAge( age );
	ply:SetPlayerMoney( 200 );
	
	ply:SetPlayerTitle( title );
	ply:SetNWString( "title", title );
	
	ply:SetPlayerSecondTitle( title2 )
	ply:SetNWString( "secondtitle", title2 );
	
	ply:SetPlayerFaction( faction );
	
	ply:GetTable().CitizenModel = model
	ply:GetTable().CCModel = model
	
	ply:SetModel( model )

	umsg.Start( "RCM", ply ) umsg.End()

	ply:GetTable().LastCharCreation = CurTime();
	ply:GetTable().CharCreateMenu = false

	if( not ply:GetTable().HasSeenMOTD ) then
	
		ply:GetTable().CanInitialize = true;
		ply:PromptMOTD();
	
	else
	
		ply:CharacterInitialize();
		
		if( ply:GetTable().DeletedSave ) then
			ply:GetTable().DeletedSave = false;
		end
	
	end
	
	ply:SaveCharacter();	
	
	timer.Simple( 50, ply.UpdateCharacter, ply );
	
	if( ply:GetTable().InGame ) then
		timer.Simple( .3, function()
			ply:RefreshShit()
			ply:HandleTeamSpawn()
			--ply:PrintMessage( 3, "refreshed!" );
		end )	
	end	
	
end
concommand.Add( "eng_createcharacter", ccCreateCharacter );

function engCreateAccount( ply, cmd, arg )

	local new = "INSERT INTO 'users' "
	
	local field = { }
	
	field["UserName"] = arg[1]
	field["SteamID"] = ply:SteamID()
	
	new = new .. "( "

	local comma = false;

	for k, v in pairs( field ) do
	
		if( comma ) then
			new = new .. ", "
		end
	
		new = new .. k
		comma = true
	
	end
	
	new = new .. ") VALUES ( ";
	
	local comma = false
	
	for k, v in pairs( field ) do
	
		if( comma ) then
			new = new .. ", "
		end
	
		new = new .. "'" .. v .. "'"
		comma = true;
	
	end	

	new = new .. "); "

	sql.Query( new )
		
	umsg.Start( "RAM", ply ) umsg.End()
	
	ply:CallEvent( "StartMenu" );
	
end	
concommand.Add( "eng_accountcreation", engCreateAccount )

function engDeleteCharacter( ply, cmd, arg )

	if( !arg[2] ) then
		return;	
	end

	local uniqueid = arg[1]

	if( tonumber( uniqueid ) ~= ply:GetField( "UniqueID" ) ) then 
		return 
	end
		
	local id = tonumber( arg[2] );
		
	sql.Query( "DELETE FROM `characters` WHERE SteamID = '" .. ply:SteamID() .. "' AND id = '" .. id .. "';" );
	
	print( "removed save " .. id );
	
end
concommand.Add( "eng_removechar", engDeleteCharacter ) 	

function ccSelectChar( ply, cmd, arg )

	if( not arg[1] ) then
		return	
	end	

	local uniqueid = arg[1];

	if( tonumber( uniqueid ) ~= ply:GetField( "UniqueID" ) ) then return; end
		
	local id = tonumber( arg[2] );
	ply:GetTable().LastCharCreation = CurTime();
	ply:GetTable().OutOfMenu = true
	
	umsg.Start( "RCCM", ply ) umsg.End();
	
	ply:SetPlayerChosenCharID( id );
	ply:CharacterLoadInitialize();
	
	--ply:SetNWString( "RPName", arg[3] );

	timer.Simple( 4, function()
	
		ply:SetPlayerChosenCharID( -1 )
		
	end )	
	
	umsg.Start( "LCD", ply ) umsg.End();
	
	umsg.Start( "FIFO", ply );
		umsg.Float( 5 );
	umsg.End();
		
end
concommand.Add( "eng_selectchar", ccSelectChar );

function CloseMOTD( ply, cmd, arg )

	if( not ply:GetTable().Initialized and
			ply:GetTable().CanInitialize ) then
	
		umsg.Start( "FIFO", ply );
			umsg.Float( 5 );
		umsg.End();
		
		timer.Simple( 2, ply.CharacterInitialize, ply );
	
	end
	
end
concommand.Add( "rp_closemotd", CloseMOTD );

function ccIsTyping( ply, cmd, arg )

	ply:SetNWBool( "IsTyping", true );

end
concommand.Add( "eng_istyping", ccIsTyping );

function ccIsNotTyping( ply, cmd, arg )

	ply:SetNWBool( "IsTyping", false );

end
concommand.Add( "eng_isnottyping", ccIsNotTyping );

function ccDontUseThisCommand( ply, cmd, arg )

	ply:PrintMessage( 2, "Attempted to use some command you shouldn't have\n" );

	if( ply:EntIndex() == 0 ) then
	
		ply:PrintMessage( 2, "NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!\n" );
	
	end

end
concommand.Add( "dontusethiscommand", ccDontUseThisCommand );

function ccReceiveItemInfo( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		ply:Disconnect();
		return;
	end

	local ent = ents.GetByIndex( tonumber( arg[1] ) );

	if( not ent or not ent:IsValid() ) then return; end

	if( not ent:GetTable().ItemID ) then return; end	
	
	if( ply:GetTable().SendItemNames[ent:EntIndex()] ) then
	
		if( ply:GetTable().SendItemNames[ent:EntIndex()].id == ent:GetTable().ItemID and
			ply:GetTable().SendItemNames[ent:EntIndex()].fid == ent:GetTable().IFID ) then
			return;
		end
		
	end
	
	ply:GetTable().SendItemNames[ent:EntIndex()] = { id = ent:GetTable().ItemID, fid = ent:GetTable().IFID };
	
	umsg.Start( "RIHI", ply );
		umsg.Short( tonumber( arg[1] ) );
		umsg.String( TS.ItemsData[ent:GetTable().ItemID].Name );
	umsg.End();

end
concommand.Add( "eng_reciteminfo",  ccReceiveItemInfo );

function ccInventoryDropItem( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[2] ) or not tonumber( arg[3] ) or not tonumber( arg[4] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = arg[1];
	local iid = tonumber( arg[2] );
	local x = tonumber( arg[3] );
	local y = tonumber( arg[4] );
	
	if( not ply:GetTable().InventoryGrid[iid][x][y].Filled ) then return; end
	
	local dropitemthink = function( ply, done )
		
		if( done ) then
			
			if( not ply:GetTable().InventoryGrid[iid][x][y].Filled ) then return; end

			if( not ply:GetTable().InventoryGrid[iid][x][y].ItemData ) then return; end

			ply:GetTable().InventoryGrid[iid][x][y].ItemData.Owner = ply;
			ply:GetTable().InventoryGrid[iid][x][y].ItemData.Drop( ply:GetTable().InventoryGrid[iid][x][y].ItemData );

			local data = { }

			for k, v in pairs( ply:GetTable().InventoryGrid[iid][x][y].ItemData ) do
			
				data[k] = v;
			
			end

			DestroyProcessBar( "dropitem", ply );
			ply:TakeItemAt( iid, x, y );
			local ent = ply:DropItemProp( id );
			
			ent:GetTable().ItemData = data;
			
			return;			
		
		end
	
	end
	
	CreateProcessBar( "dropitem", "Dropping item", ply );
		SetEstimatedTime( .5, ply );
		SetThinkDelay( .25, ply );
		SetThink( dropitemthink, ply );
	EndProcessBar( ply );		

end
concommand.Add( "eng_invdropitem", ccInventoryDropItem );

function ccInventoryTakeInventoryStorageItem( ply, cmd, arg )

	if( #arg ~= 7 ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = arg[1];
	local iid = tonumber( arg[2] );
	local x = tonumber( arg[3] );
	local y = tonumber( arg[4] );
	local i_id = arg[5];
	local i_x = tonumber( arg[6] );
	local i_y = tonumber( arg[7] );

	if( not ply:GetTable().InventoryGrid[iid][x][y].Filled ) then return; end
	if( ply:GetTable().InventoryGrid[iid][x][y].ItemData.ID ~= id ) then return; end
	if( not ply:GetTable().InventoryGrid[iid][x][y].ItemData:IsContainer() ) then return; end
	
	if( ply:CanItemFitInAnyInventory( i_id ) ) then
	
		ply:GetTable().InventoryGrid[iid][x][y].ItemData:RemoveItemAt( i_x, i_y, ply );
		ply:GiveAnyInventoryItem( i_id );

	end

end
concommand.Add( "eng_invtakeinvstoritem", ccInventoryTakeInventoryStorageItem );

function ccInventoryTakeStorageItem( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[2] ) or not tonumber( arg[3] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 
	
	local id = arg[1];
	local x = tonumber( arg[2] );
	local y = tonumber( arg[3] );
	
	local ent = ply:GetTable().StorageEntity;
	
	if( not ent or not ent:IsValid() ) then
		return;
	end
	
	if( ( ent:GetPos() - ply:GetPos() ):Length() > 90 ) then
		ply:GetTable().StorageEntity = nil;
		return;
	end

	local takeitemthink = function( ply, done )
		
		if( done ) then
			
			if( ply:GetTable().StorageEntity and
				ply:GetTable().StorageEntity:IsValid() ) then
				
				local ent = ply:GetTable().StorageEntity;

				if( ent:GetTable().ItemData.InventoryGrid[x][y].ItemData and ent:GetTable().ItemData.InventoryGrid[x][y].ItemData.ID == id  and 
					ply:CanItemFitInAnyInventory( id ) ) then			
					
					ply:GiveAnyInventoryItem( ent:GetTable().ItemData.InventoryGrid[x][y].ItemData );
	
					ent:GetTable().ItemData:RemoveItemAt( x, y );

				end
				
			end
			
			DestroyProcessBar( "takeitem", ply );

			return;			
		
		end
	
	end
	
	CreateProcessBar( "takeitem", "Taking item", ply );
		SetEstimatedTime( .3, ply );
		SetThinkDelay( .3, ply );
		SetThink( takeitemthink, ply );
	EndProcessBar( ply );		


end
concommand.Add( "eng_invtakestorageitem", ccInventoryTakeStorageItem );

function ccInventoryAddStorageItem( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[2] ) or not tonumber( arg[3] ) or not tonumber( arg[4] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 
	
	if( CurTime() - ply:GetTable().LastStorageItemAdd < 1 ) then return; end

	ply:GetTable().LastStorageItemAdd = CurTime();

	local id = arg[1];
	local iid = tonumber( arg[2] );
	local x = tonumber( arg[3] );
	local y = tonumber( arg[4] );
	
	local ent = ply:GetTable().StorageEntity;
		
	if( not ent or not ent:IsValid() ) then
		return;
	end
	
	if( ( ent:GetPos() - ply:GetPos() ):Length() > 90 ) then
		ply:GetTable().StorageEntity = nil;
		return;
	end
	
	if( not ent:GetTable().ItemData:CanItemFitInInventory( id ) ) then
	
		umsg.Start( "NRIS", ply );
		umsg.End();
		return;
	
	end
	
	if( string.find( TS.ItemsData[id].Flags, "c" ) ) then
	
		return;
	
	end
	
	local additemthink = function( ply, done )
		
		if( done ) then

			if( ply:GetTable().InventoryGrid[iid][x][y].ItemData and ply:GetTable().InventoryGrid[iid][x][y].ItemData.ID == id ) then
			
				if( ent:GetTable().ItemData:CanItemFitInInventory( id ) ) then

					ent:GetTable().ItemData:GiveInventoryItem( ply:GetTable().InventoryGrid[iid][x][y].ItemData );
					ply:TakeItemAt( iid, x, y );
					
					if( string.find( id, "ts2_" ) and id ~= "ts2_hands" and not ply:HasItem( id ) ) then
						
						ply:StripWeapon( id );
						
					end

					
			
				else
				
					umsg.Start( "NRIS", ply );
					umsg.End();
				
				end
			
			end

			DestroyProcessBar( "additem", ply );

			return;			
		
		end
	
	end
	
	CreateProcessBar( "additem", "Adding item to storage", ply );
		SetEstimatedTime( .3, ply );
		SetThinkDelay( .3, ply );
		SetThink( additemthink, ply );
	EndProcessBar( ply );	

end
concommand.Add( "eng_invaddstorage", ccInventoryAddStorageItem );

function ccInventoryUseItemPortion( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[2] ) or not tonumber( arg[3] ) or not tonumber( arg[4] ) ) then
		return;
	end

	if( ply:IsTied() ) then return; end 

	local id = arg[1];
	local iid = tonumber( arg[2] );
	local x = tonumber( arg[3] );
	local y = tonumber( arg[4] );
	
	if( not ply:GetTable().InventoryGrid[iid][x][y].Filled ) then return; end
	
	local useitemthink = function( ply, done )
		
		if( done ) then
			
			ply:GetTable().InventoryGrid[iid][x][y].ItemData.Owner = ply;
			ply:GetTable().InventoryGrid[iid][x][y].ItemData.Use( ply:GetTable().InventoryGrid[iid][x][y].ItemData );
			ply:GetTable().InventoryGrid[iid][x][y].ItemData.Amount = ply:GetTable().InventoryGrid[iid][x][y].ItemData.Amount - 1;

			DestroyProcessBar( "useitem", ply );
	
			if( ply:GetTable().InventoryGrid[iid][x][y].ItemData.Amount < 1 ) then
		
				if( ( not arg[5] or tonumber( arg[5] ) ~= 1 ) and ply:GetTable().InventoryGrid[iid][x][y].ItemData.CanDrop( ply:GetTable().InventoryGrid[iid][x][y].ItemData ) ) then
					ply:TakeItemAt( iid, x, y );
				end
				
			else
			
				umsg.Start( "UIA", ply );
					umsg.Short( iid );
					umsg.Short( x );
					umsg.Short( y );
					umsg.Short( ply:GetTable().InventoryGrid[iid][x][y].ItemData.Amount );
				umsg.End();
			
			end
				
			return;			
		
		end
	
	end
	
	CreateProcessBar( "useitem", "Using item", ply );
		SetEstimatedTime( ply:GetTable().InventoryGrid[iid][x][y].ItemData.UseDelay * .4, ply );
		SetThinkDelay( .05, ply );
		SetThink( useitemthink, ply );
	EndProcessBar( ply );		

end
concommand.Add( "eng_invuseitemportion", ccInventoryUseItemPortion );

function ccInventoryUseItem( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[2] ) or not tonumber( arg[3] ) or not tonumber( arg[4] ) ) then
		return;
	end

	if( ply:IsTied() ) then return; end 

	local id = arg[1];
	local iid = tonumber( arg[2] );
	local x = tonumber( arg[3] );
	local y = tonumber( arg[4] );
	
	if( not ply:GetTable().InventoryGrid[iid][x][y].Filled ) then return; end
	
	local useitemthink = function( ply, done )
		
		if( done ) then
			
			ply:GetTable().InventoryGrid[iid][x][y].ItemData.Owner = ply;
			ply:GetTable().InventoryGrid[iid][x][y].ItemData.Use( ply:GetTable().InventoryGrid[iid][x][y].ItemData );

			DestroyProcessBar( "useitem", ply );
	
			if( ( not arg[5] or tonumber( arg[5] ) ~= 1 ) and ply:GetTable().InventoryGrid[iid][x][y].ItemData.CanDrop( ply:GetTable().InventoryGrid[iid][x][y].ItemData ) ) then
				ply:TakeItemAt( iid, x, y );
			end
			
			return;			
		
		end
	
	end
	
	CreateProcessBar( "useitem", "Using item", ply );
		SetEstimatedTime( ply:GetTable().InventoryGrid[iid][x][y].ItemData.UseDelay, ply );
		SetThinkDelay( .3, ply );
		SetThink( useitemthink, ply );
	EndProcessBar( ply );		

end
concommand.Add( "eng_invuseitem", ccInventoryUseItem );

function ccInventoryDropStorage( ply, cmd, arg )

	if( not arg[1] ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local name = arg[1];
	
	if( not ply:HasInventory( name ) ) then return; end
	
	local iid = ply:GetInventoryIndex( name );
	
	if( ply:GetTable().Inventories[iid].Permanent ) then
		return;
	end
	
	local dropstoragethink = function( ply, done )
		
		if( done ) then
		
			if( ply:GetTable().Inventories[iid].IsClothes and ply:GetTable().Inventories[iid].CanDrop ) then
			
				DestroyProcessBar( "dropstorage", ply );
				ply:DropClothes();
				ply:WearItem( "clothes_citizen" );
				
				TS.CreateWhiteNotify( ply, "Dropped Clothes" )
				
				return;
			
			end
		
			if( ply:HasInventory( name ) ) then
			
				local itemprop = ply:DropItemProp( ply:GetTable().Inventories[iid].ID );
				itemprop = TS.ItemToContainer( itemprop:GetTable().ItemData );
				
				local itemdata = TS.ItemsData[ply:GetTable().Inventories[iid].ID];
				local inv = 0;
				
				local activeweapx = -1;
				local activeweapy = -1;
				
				for k = 0, itemdata.Height - 1 do
			
					for j = 0, itemdata.Width - 1 do
					
						local port = true;
					
						if( ply:GetTable().InventoryGrid[iid][j][k].x == activeweapx and
							ply:GetTable().InventoryGrid[iid][j][k].y == activeweapy ) then
							
							port = false;
							
						end
					
						if( ply:GetTable().InventoryGrid[iid][j][k].ItemData and ply:GetActiveWeapon():IsValid() ) then
						
							if( ply:GetTable().InventoryGrid[iid][j][k].ItemData.ID == ply:GetActiveWeapon():GetClass() ) then
							
								if( ply:HasWeaponOnlyFrom( ply:GetTable().InventoryGrid[iid][j][k].ItemData.ID, iid ) ) then
								
									port = false;
									
									activeweapx = ply:GetTable().InventoryGrid[iid][j][k].x;
									activeweapy = ply:GetTable().InventoryGrid[iid][j][k].y;
									
									if( not ply:GiveAnyInventoryItemBut( ply:GetTable().InventoryGrid[iid][j][k].ItemData.ID, iid ) ) then
									
										ply:GetTable().HasTempWeapon = true;
										ply:GetTable().TempWeaponClass = ply:GetActiveWeapon():GetClass();
										
										
									end
									
									ply:GetTable().InventoryGrid[iid][j][k].ItemData = nil;
								
								end
							
							end
						
						end
					
						if( port ) then
					
							itemprop.InventoryGrid[j][k].Filled = ply:GetTable().InventoryGrid[iid][j][k].Filled;
							itemprop.InventoryGrid[j][k].SX = ply:GetTable().InventoryGrid[iid][j][k].x;
							itemprop.InventoryGrid[j][k].SY = ply:GetTable().InventoryGrid[iid][j][k].y;
							itemprop.InventoryGrid[j][k].ItemData  = ply:GetTable().InventoryGrid[iid][j][k].ItemData;

						else
						
							itemprop.InventoryGrid[j][k].Filled = false;
							itemprop.InventoryGrid[j][k].SX = -1;
							itemprop.InventoryGrid[j][k].SY = -1;
							itemprop.InventoryGrid[j][k].ItemData = nil;
							
						end

					end
				
				end
				
				if( name == "Backpack" and ply:GetTable().BackEntity and ply:GetTable().BackEntity:IsValid() ) then
					ply:GetTable().BackEntity:Remove();
				end
				
				if( name == "Turtle Hat" and ply:GetTable().HeadEntity and ply:GetTable().HeadEntity:IsValid() ) then
					ply:GetTable().HeadEntity:Remove();
				end
				
				ply:RemoveInventory( itemdata.Name );
				
			end

			DestroyProcessBar( "dropstorage", ply );
			
			return;			
		
		end
	
	end
	
	CreateProcessBar( "dropstorage", "Dropping storage item", ply );
		SetEstimatedTime( .5, ply );
		SetThinkDelay( .25, ply );
		SetThink( dropstoragethink, ply );
	EndProcessBar( ply );		

end
concommand.Add( "eng_invdropstorage", ccInventoryDropStorage );

function ccInventoryWriteItem( ply, cmd, arg )

	if( not tonumber( arg[1] ) or not tonumber( arg[2] ) or not tonumber( arg[3] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = "paper";
	local iid = tonumber( arg[1] );
	local x = tonumber( arg[2] );
	local y = tonumber( arg[3] );
	
	if( not ply:GetTable().InventoryGrid[iid][x][y].Filled ) then return; end
	if( ply:GetTable().InventoryGrid[iid][x][y].ItemData.ID ~= "paper" ) then return; end

	ply:GetTable().SelectedPaper = { Inv = iid, x = x, y = y };

	umsg.Start( "LWP", ply );
		umsg.String( "" );
	umsg.End();
		
	
end
concommand.Add( "eng_invwriteitem", ccInventoryWriteItem );

function ccActionMenuPickupContainer( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end

	if( not string.find( ent:GetTable().ItemData.Flags, "c" ) ) then return; end

	if( ply:HasContainer( ent:GetTable().ItemData.Name ) ) then
	
		ply:PrintMessage( 3, "You already have one" );
		return;
	
	end

	local pickupitemthink = function( ply, done )
		
		if( not ent or not ent:IsValid() ) then
		
			DestroyProcessBar( "pickupitem", ply );
			return;
		
		end
		
		if( done ) then
		
			if( not ply:HasContainer( ent:GetTable().ItemData.Name ) ) then
				
				if( ent:GetTable().ItemData.ID == "turtlehat" ) then
					ply:GetTable().HeadEntity = ply:AttachProp( ent:GetModel(), "anim_attachment_head" );
				end
				
				ply:GiveContainerEntity( ent, false, true );
				
				TS.CreateWhiteNotify( ply, "Picked up " .. ent:GetTable().ItemData.ID )
			
			end
		
			DestroyProcessBar( "pickupitem", ply );
			ent:Remove();
			return;			
		
		end
		
		local crntdist = ( ent:GetPos() - ply:GetPos() ):Length();
		
		if( crntdist > 90 ) then
		
			DestroyProcessBar( "pickupitem", ply );
			return;
		
		end
	
	end
		
	CreateProcessBar( "pickupitem", "Picking up item", ply );
		SetEstimatedTime( ent:GetTable().ItemData.PickupDelay, ply );
		SetThinkDelay( .1, ply );
		SetThink( pickupitemthink, ply );
	EndProcessBar( ply );
		

end
concommand.Add( "eng_ampickupcontainer", ccActionMenuPickupContainer );

function ccActionMenuLookInsideContainer( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end

	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end

	if( not string.find( ent:GetTable().ItemData.Flags, "c" ) and not string.find( ent:GetTable().ItemData.Flags, "W" ) ) then return; end

	ply:OpenStorageMenu( ent );

end
concommand.Add( "eng_amlookinsidecontainer", ccActionMenuLookInsideContainer );

function ccInventoryLookInsideContainer( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[2] ) or not tonumber( arg[3] ) or not tonumber( arg[4] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = arg[1];
	local iid = tonumber( arg[2] );
	local x = tonumber( arg[3] );
	local y = tonumber( arg[4] );
	
	if( not ply:GetTable().InventoryGrid[iid][x][y].Filled ) then return; end
	if( ply:GetTable().InventoryGrid[iid][x][y].ItemData.ID ~= id ) then return; end
		
	if( not string.find( ply:GetTable().InventoryGrid[iid][x][y].ItemData.Flags, "C" ) and not string.find( ply:GetTable().InventoryGrid[iid][x][y].ItemData.Flags, "c" ) and not string.find( ply:GetTable().InventoryGrid[iid][x][y].ItemData.Flags, "W" ) ) then return; end

	ply:OpenStorageMenu( ply:GetTable().InventoryGrid[iid][x][y].ItemData );

end
concommand.Add( "eng_invlookinsidecontainer", ccInventoryLookInsideContainer );

function ccActionMenuReadItem( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end

	if( not string.find( ent:GetTable().ItemData.Flags, "l" ) ) then return; end

	ent:GetTable().ItemData.Owner = ply;
	ent:GetTable().ItemData.Use( ent:GetTable().ItemData );	
	
end
concommand.Add( "eng_amreaditem", ccActionMenuReadItem );

function ccActionMenuUseItem( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end

	local useitemthink = function( ply, done )

		local ent = ply:GetTable().ActionMenuTarget;
		
		if( not ent or not ent:IsValid() ) then
		
			DestroyProcessBar( "useitem", ply );
			return;
		
		end
		
		if( done ) then
			
			ent:GetTable().ItemData.Owner = ply;
			ent:GetTable().ItemData.Use( ent:GetTable().ItemData );
			
			umsg.Start( "CNWM", ply )
				umsg.String( "Used " .. ent:GetTable().ItemData.Name )
			umsg.End()	

			DestroyProcessBar( "useitem", ply );
			ent:Remove();
			return;			
		
		end
		
		local crntdist = ( ent:GetPos() - ply:GetPos() ):Length();
		
		if( ply:GetTable().DistanceFromItem < crntdist ) then
		
			DestroyProcessBar( "useitem", ply );
			return;
		
		end
	
	end
	
	CreateProcessBar( "useitem", "Using item", ply );
		SetEstimatedTime( ent:GetTable().ItemData.UseDelay, ply );
		SetThinkDelay( .3, ply );
		SetThink( useitemthink, ply );
	EndProcessBar( ply );		

end
concommand.Add( "eng_amuseitem", ccActionMenuUseItem );

function ccActionMenuWearItem( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	--[[if( not ply:IsCitizen() ) then
		return;
	end]]--

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end

	local wearitemthink = function( ply, done )

		local ent = ply:GetTable().ActionMenuTarget;
		
		if( not ent or not ent:IsValid() ) then
		
			DestroyProcessBar( "wearitem", ply );
			return;
		
		end
		
		if( done ) then
			
			DestroyProcessBar( "wearitem", ply );
			
			ent:GetTable().ItemData.Owner = ply;
			ent:GetTable().ItemData.Use( ent:GetTable().ItemData );
			
			ply:DropClothes();
			ply:WearItem( ent:GetTable().ItemData );
			
			TS.CreateWhiteNotify( ply, "Put on " .. ent:GetTable().ItemData.Name )
			
			ent:Remove();
			return;			
		
		end
		
		--[[if( not ply:IsCitizen() ) then
			DestroyProcessBar( "wearitem", ply );
			return;
		end]]--
		
		local crntdist = ( ent:GetPos() - ply:GetPos() ):Length();
		
		if( ply:GetTable().DistanceFromItem < crntdist ) then
		
			DestroyProcessBar( "wearitem", ply );
			return;
		
		end
	
	end
	
	CreateProcessBar( "wearitem", "Wearing item", ply );
		SetEstimatedTime( ent:GetTable().ItemData.UseDelay, ply );
		SetThinkDelay( .3, ply );
		SetThink( wearitemthink, ply );
	EndProcessBar( ply );		

end
concommand.Add( "eng_amwearitem", ccActionMenuWearItem );

function ccPickupItem( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end
	
	local pickupitemthink = function( ply, done )

		local ent = ply:GetTable().ActionMenuTarget;
		
		if( not ent or not ent:IsValid() ) then
		
			DestroyProcessBar( "pickupitem", ply );
			return;
		
		end
		
		if( done ) then
			ply:GiveInventoryItem( id, ent:GetTable().ItemData );
			DestroyProcessBar( "pickupitem", ply );
			ent:Remove();

			TS.CreateWhiteNotify( ply, "Picked up " .. ent:GetTable().ItemData.Name )
			
			return;			
		
		end
		
		local crntdist = ( ent:GetPos() - ply:GetPos() ):Length();
		
		if( ply:GetTable().DistanceFromItem < crntdist ) then
		
			DestroyProcessBar( "pickupitem", ply );
			return;
		
		end
	
	end
	
	CreateProcessBar( "pickupitem", "Picking up item", ply );
		SetEstimatedTime( ent:GetTable().ItemData.PickupDelay, ply );
		SetThinkDelay( .1, ply );
		SetThink( pickupitemthink, ply );
	EndProcessBar( ply );

end
concommand.Add( "eng_pickupitem", ccPickupItem );

function ccHideViewModel( ply, cmd, arg )

	ply:DrawViewModel( false );

end
concommand.Add( "eng_hideviewmodel", ccHideViewModel );

function ccShowViewModel( ply, cmd, arg )

	ply:DrawViewModel( true );

end
concommand.Add( "eng_showviewmodel", ccHideViewModel );

function ccStartLetter( ply, cmd, arg )

	ply:GetTable().LetterContent = "";

end
concommand.Add( "eng_sl", ccStartLetter );

function ccSendLetterPiece( ply, cmd, arg )

	if( not arg[1] ) then return; end
	
	if( string.len( ply:GetTable().LetterContent ) >= 1024 ) then return; end

	ply:GetTable().LetterContent = ply:GetTable().LetterContent .. arg[1];

end
concommand.Add( "eng_slp", ccSendLetterPiece );

function ccEndLetter( ply, cmd, arg )
	
	if( ply:IsTied() ) then return; end 
	
	local inv = ply:GetTable().SelectedPaper.Inv;
	local x = ply:GetTable().SelectedPaper.x;
	local y = ply:GetTable().SelectedPaper.y;
	
	if( not inv or not x or not y ) then return; end
	
	if( not ply:GetTable().InventoryGrid[inv] or not ply:GetTable().InventoryGrid[inv][x] or not ply:GetTable().InventoryGrid[inv][x][y] or ply:GetTable().InventoryGrid[inv][x][y].ItemData.ID ~= "paper" ) then
		return;
	end	

	ITEM = nil;
	ITEM = { }
	
	ITEM.ID = "letter" .. TS.LetterCount;
	ITEM.Flags = "l";
	ITEM.UseDelay = .1;
	ITEM.PickupDelay = .1;
	
	ITEM.Name = "Note";
	ITEM.Description = string.sub( string.gsub( ply:GetTable().LetterContent, "@n", "" ), 1, 30 ) .. "...";
	ITEM.Model = TS.ItemsData["paper"].Model;
	ITEM.CamPos = TS.ItemsData["paper"].CamPos;
	ITEM.LookAt = TS.ItemsData["paper"].LookAt;
	ITEM.FOV = TS.ItemsData["paper"].FOV;
	ITEM.Width = 1;
	ITEM.Height = 1;
	ITEM.LetterContent = ply:GetTable().LetterContent;
	
	ITEM.Pickup = function( self ) end
	
	ITEM.Drop = function( self ) end
	
	ITEM.Use = function( self ) 

		umsg.Start( "SL", self.Owner ); umsg.End();
		umsg.Start( "DL", self.Owner ); umsg.End();
		
		local parts = math.ceil( string.len( self.LetterContent ) / 200 );
		
		for n = 1, parts do
		
			local f = function()
			
				if( not self.Owner or not self.Owner:IsValid() ) then return; end
			
				umsg.Start( "SLP", self.Owner );
					umsg.String( string.sub( self.LetterContent, ( n - 1 ) * 200 + 1, n * 200 ) );
				umsg.End();
				
			end
			timer.Simple( n * .2, f );
			
		end
		
		
	end

	TS.ItemsData[ITEM.ID] = ITEM;
	
	ply:DropItemProp( "letter" .. TS.LetterCount );
	ply:TakeItemAt( inv, x, y );
	
	TS.LetterCount = TS.LetterCount + 1;

end
concommand.Add( "eng_el", ccEndLetter );

function ccUpdateTargetID( ply, cmd, arg )

	if( CurTime() - ply:GetTable().LastTargetIDUpdate < 1 ) then
	
		return;
	
	end
	
	ply:GetTable().LastTargetIDUpdate = CurTime();
	
	if( not tonumber( arg[1] ) ) then
	
		return;
	
	end
	
	local ent = ents.GetByIndex( ( tonumber( arg[1] ) ) );
	
	if( ent:IsDoor() ) then
	
		if( not arg[2] or not arg[3] ) then
			
			return;
			
		end
	
	elseif( ent:IsPlayer() ) then
	
		if( not arg[2] ) then
		
			return;
		
		end
	
	else
	
		return;
	
	end
	
	if( ent and ent:IsValid() ) then
	
		if( ent:IsPlayer() ) then
		
			local title = ent:GetPlayerTitle();
			local title2 = ent:GetNWString( "secondtitle" );
			
			if( not title ) then
				title = "";
			end
			
			if( not title2 ) then
				title2 = "";
			end	
			
			if( ent:IsTied() ) then
			
				title = title .. "\n(Tied)";
			
			end
			
			if( arg[2] ~= title ) then
			
				umsg.Start( "UDPT", ply );
					umsg.Entity( ent );
					umsg.String( title );
				umsg.End();
			
			end
			
			if( arg[3] ~= title2 ) then
			
				umsg.Start( "UDPST", ply );
					umsg.Entity( ent );
					umsg.String( title2 );
				umsg.End();
			
			end
		
		elseif( ent:IsDoor() ) then
	
			local propertyname = ent:GetTable().PropertyName;
			local doorname = ent:GetTable().DoorName;
			
			if( propertyname ~= arg[2] or doorname ~= arg[3] ) then
			
				umsg.Start( "UDD", ply );
					umsg.Entity( ent );
					umsg.String( propertyname or "" );
					umsg.String( doorname or "" );
				umsg.End();
			
			end
	
		end
	
	end
	
end
concommand.Add( "eng_uti", ccUpdateTargetID );

function ccDoRealPrimaryAttack( ply, cmd, arg )

	if( ply:IsTied() ) then return; end 

	local weap = ply:GetActiveWeapon();
	
	if( weap and weap:IsValid() and weap:GetTable().RealPrimaryAttack ) then
	
		weap:GetTable():RealPrimaryAttack( ply );
	
	end

end
concommand.Add( "eng_dorealprimaryattack", ccDoRealPrimaryAttack );

function ccLookInsideMapStorage( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end
	
	if( ent:IsDoor() and ent:GetTable().DoorFlags and string.find( ent:GetTable().DoorFlags, "s" ) ) then
	
		ply:OpenStorageMenu( ent );
	
	end

	

end
concommand.Add( "eng_lookinsidemapstorage", ccLookInsideMapStorage );

function ccBuyStorage( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end
	
	if( ent:IsDoor() and ply:CanOwnDoor( ent ) ) then
	
		--ply:PurchaseProperty( ent, id );
		ply:OwnDoor( ent );
		ply:TakeMoney( id );
		
		
	end

end
concommand.Add( "eng_buystorage", ccBuyStorage );

function ccRentProperty( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end
	
	if( ent:IsDoor() and ply:CanOwnDoor( ent ) ) then
		
		if( id == -1 and ply:GetTable().OwnsFreeProperty ) then
			ply:PrintMessage( 3, "You can only own one free property at a time!" );
		else
		--ply:PurchaseProperty( ent, id );
		ply:OwnProperty( ent, id );
		end
		
	end

end
concommand.Add( "eng_rentproperty", ccRentProperty );

function ccSellProperty( ply, cmd, arg )

	if( not arg[1] or not tonumber( arg[1] ) ) then
		return;
	end
	
	if( ply:IsTied() ) then return; end 

	local id = tonumber( arg[1] );
	
	local ent = ply:GetTable().ActionMenuTarget;
	
	if( not ent or not ent:IsValid() ) then return; end
	
	ply:GetTable().DistanceFromItem = ( ent:GetPos() - ply:GetPos() ):Length();

	if( ply:GetTable().DistanceFromItem > 90 ) then
	
		return;
	
	end
	
	if( ent:IsDoor() ) then
		if( ent:GetTable().MainOwner == ply ) then
		ent:UnownProperty( ply );
		if( id == 0 ) then
			idmsg = "nothing, this property was free!"
		else
			idmsg = id .. " credits!"
		end
		ply:GiveMoney( id );
		ply:PrintMessage( 3, "You have been refunded " .. idmsg );
		else
		ply:PrintMessage( 3, "You are not the main owner, you cannot cancel the rent!" );
		end
	end

end
concommand.Add( "eng_sellproperty", ccSellProperty );

function ccSeeScope( ply, cmd, arg )

	if( not tonumber( arg[1] ) ) then return; end
	
	local val = tonumber( arg[1] );

	if( val == 1 ) then

		if( ply:GetActiveWeapon():IsValid() and
			ply:GetActiveWeapon():GetClass() == "ts2_cisr" or ply:GetActiveWeapon():GetClass() == "ts2_svd" or ply:GetActiveWeapon():GetClass() == "ts2_ep2sniper" or ply:GetActiveWeapon():GetClass() == "ts2_aug" ) then
			
			ply:SetFOV( 20, 0 );
			ply:DrawViewModel( false );
			
		end
			
	else
	
		ply:SetFOV( 75, 0 );
		ply:DrawViewModel( true );		
	
	end

end
concommand.Add( "eng_seescope", ccSeeScope );

function ccGiveSavedItem( ply, cmd, arg )
	
	local id = arg[1]
	
	ply:GiveAnyInventoryItem( id );
	if( string.find( id, "ts2_" ) ) then
		ply:Give( id );
	end
	return;			
	
end
concommand.Add( "eng_givesaveditem", ccGiveSavedItem );

function ccGiveSavedBackPack( ply )	

	local id = "backpack"
	
	local name = TS.ItemsData[id].Name;
	local w = TS.ItemsData[id].InvWidth;
	local h = TS.ItemsData[id].InvHeight;
	
		local iid = ply:AddInventory( name, w, h, false, true );

		ply:GetTable().Inventories[iid].ID = id; 
			
		
end
concommand.Add( "eng_givesavedbackpack", ccGiveSavedBackPack );
		